import {getAffiliate, postAffiliate, putAffiliate} from '@/base/api/AffiliateApi';
import {Affiliate} from '@/base/model/Affiliate';

export class FormDetailBloc {
  form = new Affiliate();

  load(id: number) {
    if (id === 0) {
      this.form.sex = '男';
      return;
    }
    return getAffiliate(id)
      .then((res) => {
        this.form = res.data;
        this.form.sex = res.data.sex === 'M' ? '男' : '女';
      });
  }

  save(id: number | '', memberId) {
    const entity: any = {
      nickname: this.form.nickname,
      birthday: this.form.birthday,
      height: this.form.height,
      sex: this.form.sex === '男' ? 'M' : 'F',
      weight: this.form.weight
    };
    if (!id) {
      entity.memberId = memberId;
    }
    return id ? putAffiliate(id, entity) : postAffiliate(entity);
  }

  reset() {
    this.form = new Affiliate();
  }
}
